import axios from "axios"
const state={
    adminName:localStorage.adminName,
    token:localStorage.token,
    adminLog:[],
    adminInfo:[],

}
const mutations={
    CHANGE_INFO(state,{adminName,token}){
        state.adminName=localStorage.adminName= adminName
        state.token=localStorage.token=token
    },
    CHANGE_FORM(state,adminForm){
        state.adminForm=adminForm
    },
    CHANGE_FALSE(state){
      localStorage.clear()
        state.adminName=state.token=null
    },
    CHANGE_LOG(state,adminLog){
        state.adminLog=adminLog
    },
    CHANGE_DATA(state,adminInfo){
        state.adminInfo=adminInfo
    }
}

const actions={
    addAdminForm({dispatch},params){
        return axios.post("/adminList",params)

    },
    login({commit},that){
        axios.post('/login',that.adminInfo)
            .then(data=>{
                if(data.ok===1){

                    commit("CHANGE_INFO",data)
                    that.$message({
                        message: that.adminInfo.adminName+'登陆成功',
                        type: 'success'
                    });
                }else{
                    that.$message({
                        showClose: true,
                        message: '账号或密码错误',
                        type: 'error',
                        duration:1500
                    });
                }
            })
    },
    getAdminLog({commit},params){
            axios.get("/adminLog",{
                params
            })
                .then(data=>{
                    commit("CHANGE_LOG",data.adminLog)
                    // commit("CHANGE_PAGE",data.pageSum)   //设置总页数
                })
    },
    getAdminData({commit},params){
        axios.get("/adminList",{
            params
        })
            .then(data=>{
                commit("CHANGE_DATA",data.adminInfo)
            })
    },
    deleteAdmin({dispatch},id){
        axios.delete("/adminLog/"+id,{
            params:{
                id
            }
        })
            .then(data=>{
                if(data.ok===1)dispatch("getAdminLog")
            })
    },
    deleteAdminUser({dispatch},id){
        axios.delete("/adminList/"+id,{
            params:{
                id
            }
        })
            .then(data=>{
                if(data.ok===1)dispatch("getAdminData")
            })
    }
}

export default {
    state,
    actions,
    mutations,

}